import { ConfigEnv, UserConfig, defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "@vant/auto-import-resolver";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { viteMockServe } from "vite-plugin-mock";

export default defineConfig((config: ConfigEnv): UserConfig => {
  const command = config.command;
  console.log('[01] config env command is:', command);
  const mode = config.mode;
  console.log('[02] config env mode is:', mode);
  const envDir = process.cwd();
  console.log('[03] process cwd is:', envDir);
  const env = loadEnv(mode, envDir);
  console.log('[04] load env is:', env);
  return {
    base: "/",
    plugins: [
      vue(),
      Components({
         resolvers: [VantResolver()],
      }),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(envDir, "src/assets/icons")],
        symbolId: "icon-[dir]-[name]",
      }),
      viteMockServe({
        mockPath: "mock",
        enable: command === "serve", // 开发阶段使用MockJS数据
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src"),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: `@import "./src/styles/variable.scss";`,
          // additionalData: undefined,
          // api: "modern-compiler",
        },
      },
    },
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
});
